package xyz.lwm.lazycat.utility.metaobject;

import lombok.Getter;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 复制属性的配置信息
 *
 * @author lwm
 */
@Getter
public class MetaConfig {

    // 忽略属性 (源对象的属性名称)
    private final Set<String> ignoreProperties = new HashSet<>();

    // 属性名映射 (不同名称也可以映射) (k: 源属性名称; v: 目标属性名称)
    private final Map<String, String> propertyMappings = new HashMap<>();

    // 忽略 null 属性
    private boolean ignoreNull;

    // 自动转换类型
    private boolean autoConvert;

    // 错误继续 (设置值时出错, 是否继续下一步)
    private boolean continueOnerror;

    public static MetaConfig create() {
        return new MetaConfig();
    }

    public MetaConfig ignoreNull() {
        this.ignoreNull = true;
        return this;
    }

    public MetaConfig autoConvert() {
        this.autoConvert = true;
        return this;
    }

    public MetaConfig continueOnerror() {
        this.continueOnerror = true;
        return this;
    }

    public MetaConfig ignoreProperties(String... ignoreProperties) {
        this.ignoreProperties.addAll(Arrays.asList(ignoreProperties));
        return this;
    }

    public MetaConfig propertyMappings(String propertyName, String targetPropertyName) {
        this.propertyMappings.put(propertyName, targetPropertyName);
        return this;
    }

    public boolean isIgnoreProperty(String propertyName) {
        return !ignoreProperties.isEmpty() && ignoreProperties.contains(propertyName);
    }

    public String getTargetPropertyName(String propertyName) {
        return propertyMappings.isEmpty() ? propertyName :
                propertyMappings.getOrDefault(propertyName, propertyName);
    }

}